package com.testingedu.filter;

import java.util.Stack;

public class ActionInvokePool {
	private static Stack<AdminActionInvoke> adminActionPool = new Stack<AdminActionInvoke>();
	private static Stack<FrontActionInvoke> frontActionPool = new Stack<FrontActionInvoke>();
	//private static final int ADMIN_ACTION_POOL_MAX = 200;//这些对象生成池的大小最好从配置文件读取
	private static final int ADMIN_ACTION_POOL_MIN = 2;
	//private static final int FRONT_ACTION_POOL_MAX = 400;
	private static final int FRONT_ACTION_POOL_MIN = 4;
	private static final int CREATE_NUM = 5;
	private static boolean isInit = false;
	
	/**
	 * 初始化对象池
	 */
	public static void init() {
		if(!isInit) {
			for(int i = 0; i < ADMIN_ACTION_POOL_MIN; i++) {
				adminActionPool.push(newAdminActionInvoke());
			}
			for(int i = 0; i < FRONT_ACTION_POOL_MIN; i++) {
				frontActionPool.push(newFrontActionInvoke());
			}
			isInit = true;
		}
	}
	
	public static synchronized AdminActionInvoke getAdminActionInvoke() {
		if(adminActionPool.isEmpty()) {
			for(int i = 0; i < CREATE_NUM; i++)
				adminActionPool.push(newAdminActionInvoke());
		}
		return adminActionPool.pop();
	}
	
	public static synchronized FrontActionInvoke getFrontActionInvoke() {
		if(adminActionPool.isEmpty()) {
			for(int i = 0; i < CREATE_NUM; i++)
				frontActionPool.push(newFrontActionInvoke());
		}
		return frontActionPool.pop();
	}
	
	public static synchronized void recoverAdminActionInvoke(AdminActionInvoke aai) {
		//if(a)
	}
	
	private static AdminActionInvoke newAdminActionInvoke() {
		return new AdminActionInvoke();
	}
	private static FrontActionInvoke newFrontActionInvoke() {
		return new FrontActionInvoke();
	}
}
